<?php include PATH_TPL."/admin/tpl.header.phtml"?>
<div class="main-container inner">
    <div class="main-content">
        <div class="container-fluid">
            <div class="row bt">
                <div class="col-lg-12 bg">
                    <ol class="breadcrumb">
                        <li class="active">
                        <i class="fa  fa-fw fa-reorder"></i>用药管理
                        </li>
                    </ol>
                </div>
            </div>
            <div class="cent">
    <span class="bg" style="position: absolute;width: 100%;height: 100%;background: black;
    opacity: 0.3;z-index: 10;padding: 0;display: none"></span>
            <form class="myForm" action="/admin_eatmedicine/add" method="post">
            <div class="column">
                <div class="text-center"><h4>添加用药</h4></div>
                <a class="btn btn-blue" id="addBtn">新增</a>
                <a class="btn btn-blue" id="save">确定</a>
                <a class="btn btn-blue" href="/admin_eatmedicine">取消</a>
                <span style="margin-left:100px;">备注：</span>
                <input type="text" name="remarks" value="" />
            </div>
            <div style="min-width:1200px;">
                <div class="live panel-body text-center">
                    <!--<div class="medicine" style="margin-top:40px;margin-left:40px;float:left;border:1px solid red;width:404px;height:204px;">
                        <div class="medicine-left" style="float:left;border:1px solid green;width:200px;height:200px;"> 
                            <span>药名：</span>
                            <select name="medicine_name" style="width:120px;">
                                <option value="0">请选择</option>
                                <?foreach($tMLDatas as $tMLRow){?>
                                <option value="<?=$tMLRow['id']?>"><?=$tMLRow['name']?></option>
                                <?}?>
                            </select><br><br><br>
                            <span>用量：</span>
                            <input type="number" style="width:50px;" name="num" value="1"/>
                            <select name="unit" style="width:80px;">
                                <option value="0">请选择</option>
                            </select><br><br><br>
                            <span>用药时间：</span>
                            <select name="medicine_name" style="width:120px;">
                                <option value="0">请选择</option>
                            </select><br><br>
                        </div>
                        <div class="medicine-right" style="float:left;border:1px solid yellow;width:200px;height:200px;"> 
                            <img class="medicine_img" src="" style="width:200px;height:200px;" />
                        </div>
                </div>-->
            </div>
            </form>
        </div>
        <div class="subviews">
            <div class="subviews-container"></div>
        </div>
    </div>
    <!-- end: PAGE -->
</div>
<!-- end: MAIN CONTAINER -->
<script type="text/javascript" src="/plugins/datepicker/WdatePicker.js" charset="UTF-8"></script>
<script>
$(function(){
    var options={
        beforeSubmit : showRequest,    // 提交前的回调函数
        success : showResponse,    // 提交后的回调函数
        dataType : "json",    // html（默认）、xml、script、json接受服务器端返回的类型
    }
    function showRequest(formData, jqForm, options){
        return true;
    }
    function showResponse(responseText,statusText){
        cg_alert_add(responseText,'/admin_medicine','/admin_medicine/add');
    }
    $('#addsubmit').click(function() {
        $('form').ajaxSubmit(options);
        return false;

    });
});
$('#search').click(function(){
    $('.form').submit();
});
        var k = 1;
$('#addBtn').on('click',function(){
        $('.live').append('<form class="myform'+k+'"><div class="medicine" data="'+k+'" style="margin-top:40px;margin-left:40px;float:left;width:404px;height:204px;"><div class="medicine-left" style="float:left;width:200px;height:200px;"><span>老人姓名：</span><select name="sid" style="width:120px;"><option value="0">请选择</option><?foreach($tSDatas as $tSRow){?><option value="<?=$tSRow['id']?>"><?=$tSRow['name']?></option><?}?></select><br><br><span>药名：</span><select name="medicine_name" style="width:120px;"><option value="0">请选择</option><?foreach($tMLDatas as $tMLRow){?><option value="<?=$tMLRow['id']?>"><?=$tMLRow['name']?></option><?}?></select><br><br><span>用量：</span><input type="number" style="width:50px;" name="num" value="1"/><select name="unit" style="width:80px;"><option value="0">请选择</option><option value="1">片</option><option value="2">支</option><option value="3">盒</option><option value="4">粒</option><option value="5">甁</option></select><br><br><span>用药时间：</span><select name="at" style="width:120px;"><option value="0">请选择</option><option value="1">早上</option><option value="2">上午</option><option value="3">中午</option><option value="4">下午</option><option value="5">晚上</option><option value="6">其他</option><option value="7">临睡用药</option></select><br><br></div><div class="medicine-right" style="float:left;width:200px;height:200px;"><img src="" style="width:200px;height:200px;" class="medicine_img" /></div></div></form>');
        k++;
    $('.medicine_img').each(function(){
        $(this).click(function(){
            if(confirm('确定删除吗?')){
                $(this).parent().parent().remove();
            }
        }); 
    });
    $('select[name="medicine_name"]').each(function(){
        var data = '1';
        $(this).on('change',function(){
            var mId = $(this).val();
            /*$.post('/admin_eatmedicine/add',{mId:mId},function(obj){
                data = obj.data;
            });*/
            $.ajax({
                type:"get",
                url:"/admin_eatmedicine/add",
                data:{mId:mId},
                async:false,
                success:function(obj){
                    data = obj.data;
                },
                error:function(data,obj){
                
                }
            });
            console.log(data);
            $(this).parent().siblings('.medicine-right').find('img').attr('src',data.img);
        });
    });
});
    $('#save').click(function(){
        var k = true;
        var j = 1;    
        $('.medicine').each(function(){
            var arr = $('.myform'+j).serialize();
            arr += '&remarks='+$('input[name="remarks"]').val();
            /*$.post('/admin_eatmedicine/add',arr,function(obj){
                if(obj.status == 0){
                    alert(obj.msg);return;
                }
        });*/
            $.ajax({
                type:"post",
                async:false,
                data:arr,
                url:'/admin_eatmedicine/add',
                success:function(obj){
                    if(obj.status == 0){
                        k = false;    
                        alert(obj.msg);return;

                    }
                },
            });
            j++;
        });
        if(k){
            alert('添加成功');location.href="/admin_eatmedicine";
        }    
    });
$('.medicine_img').each(function(){
    $(this).click(function(){
        $(this).parent().parent().remove();
    }); 
});
$('select[name="medicine_name"]').each(function(){
    $(this).on('change',function(){
        alert($(this).val())
    });
});
/*获取地址栏参数*/
function GetQuery(key){
	var search = location.search.slice(1);//得到get方式提交的查询字符串
	var arr = search.split("&");
	for(var i = 0; i < arr.length; i++){
		var ar = arr[i].split("=");
		if(ar[0] == key){
			return ar[1];
		}
	}
}

    var color="#eeeeee";
    $("#table_list tr:odd td").css("background-color",color);  //改变偶数行背景色
    /* 把背景色保存到属性中 */
    $("#table_list tr:odd").attr("bg",color);
</script>
<?php include PATH_TPL."/admin/tpl.footer.phtml"?>
